package cn.yi.medicalsys.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yi.medicalsys.pojo.Hospital;
import cn.yi.medicalsys.service.HospitalService;
import cn.yi.medicalsys.service.impl.HospitalServiceImpl;

public class HospitalServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	//通过医院编号显示医院
	public void showHospitalByNo(HttpServletRequest request, HttpServletResponse response) throws IOException{
		HospitalService hospital=new HospitalServiceImpl();
		HttpSession session = request.getSession();
		String hid=request.getParameter("hid");
		Integer hospno=null;
		if(hid!=null && !hid.equals("")){
			hospno=Integer.valueOf(hid);
		}
		Hospital hosInfo=hospital.findHospitailByNo(hospno);
		session.setAttribute("hosInfo", hosInfo);
		response.sendRedirect("perfectalter.jsp");
	}
	
	//修改医院信息
	public void updateHospital(HttpServletRequest request, HttpServletResponse response) throws IOException{
		HospitalService hospital=new HospitalServiceImpl();
		PrintWriter out = response.getWriter();
		String hid=request.getParameter("hid");
		String hlianxi=request.getParameter("hlianxi");
		String hemail=request.getParameter("hemail");
		String hintr=request.getParameter("hintr");
		String hlear=request.getParameter("hlear");
		Hospital hosp=new Hospital(Integer.valueOf(hid),hlianxi,hemail,hintr,hlear);
		boolean flag=hospital.updateHospitalByNo(hosp);
		if(flag){
			out.print("<script>alert('修改成功');</script>");
			response.sendRedirect("manage/hospital/index.jsp");
		}else{
			out.print("<script>alert('修改成功');history.back()</script>");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
		if(action!=null && !action.equals("")){
			if(action.equals("showbyid")){
				this.showHospitalByNo(request, response);
			}else if(action.equals("updatehosp")){
				this.updateHospital(request, response);
			}
		}								
		
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
	}

}
